<?php

// +----------------------------------------------------------------------

// | Description: 客户

// +----------------------------------------------------------------------

// | Author: zl

// +----------------------------------------------------------------------



namespace app\crm\controller;


use app\admin\controller\ApiCommon;

use app\crm\model\BalanceLog;

use app\crm\model\Setting;

use app\crm\model\SettingCategory;

use app\crm\model\Service;

use app\crm\model\ReturnVist;

use app\crm\model\ReturnVistT;

use app\crm\model\Customer as CustomerModel;

use app\crm\model\TransferShop;

use app\crm\model\CustomerConfig as CustomerConfigModel;

use app\crm\model\CustomerGrade as CustomerGradeModel;

use app\crm\model\ReserveandLost;

use think\response\Json;

use think\facade\Db;

use think\facade\Request;

use app\admin\model\Coupon as CouponModel;

use app\mini\model\MiniStisticsModel;

use app\common\traits\ActionRecordTrait;

use app\shop\model\unionpay\AppConfig;
use app\shop\model\unionpay\AppUtil;
use app\shop\model\unionpay\Test;


class Ztool extends ApiCommon
{
    protected $customerModel;

    protected $customerConfigModel;

    /**

     * 用于判断权限

     * @permission 无限制

     * @allow 登录用户可访问

     * @other 其他根据系统设置

     **/

    public function initialize()

    {

        $action = [

            'permission' => ['check_service_log','jiameng','export_jiameng'],

            'allow' => ['users', 'info']

        ];

        event('CheckAuth',$action);

        $request = $this->request;

        $a = strtolower($request->action());



        $this->customerModel = new CustomerModel();

        $this->customerConfigModel = new CustomerConfigModel();

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }

    }

    public function check_service_log(){
        echo "检测核销记录";

        $shop_id = [76520948050011]; // zhenghongcheng

        $staff_ids = [498];
        
        $start_time = strtotime('2023-06-01');

        $end_time = strtotime('2023-06-30 23:59:59');

        $list1 = Db::name('service_log')->where([['shop_id', 'in', $shop_id], ['nurse_id_one', 'in', $staff_ids], ['nurse_id_two', '=', 0], ['create_time', 'between', "$start_time,$end_time"]])->group('nurse_id_one')->column('count(*) as sum','nurse_id_one');

        echo "<pre>";
        print_r($list1);
        echo "</pre>";
        // exit;

        $list2 = Db::name('service_log')->where([['shop_id', 'in', $shop_id], ['nurse_id_one', 'in', $staff_ids], ['nurse_id_two', '>', 0], ['create_time', 'between', "$start_time,$end_time"]])->group('nurse_id_one')->column('count(*)*0.5 as sum','nurse_id_one');

        echo "<pre>";
        print_r($list2);
        echo "</pre>";

        $list3 = Db::name('service_log')->where([['shop_id', 'in', $shop_id], ['nurse_id_one', '>', 0], ['nurse_id_two', 'in', $staff_ids], ['create_time', 'between', "$start_time,$end_time"]])->group('nurse_id_two')->column('count(*)*0.5 as sum','nurse_id_two');

        echo "<pre>";
        print_r($list3);
        echo "</pre>";

    }

    public function jiameng(){

        $request = request()->param();

        $name = $request['name'] ?? '';

        $address = $request['address'] ?? '';

        $phone = $request['phone'] ?? '';

        $time = $request['time'] ?? '';

        if(empty($name)){
            return resultArray(['data' => [],'error' => '请填写姓名']);
        }

        if(empty($address)){
            return resultArray(['data' => [],'error' => '请填写地址']);
        }

        if(empty($phone)){
            return resultArray(['data' => [],'error' => '请填写电话']);
        }

        if(empty($time)){
            return resultArray(['data' => [],'error' => '请填写时间']);
        }
        
        $info = Db::name('jia_meng')->where('phone','=',$phone)->find();

        if($info){
            return resultArray(['data' => [],'error' => '您已经填写过信息了，我们会通知工作人员尽快联系您!']);
        }

        $addData = [
            'name'    => $name,
            'address' => $address,
            'phone'   => $phone,
            'time'    => $time,
            'create_time' => time(),
        ];

        $resId = Db::name('jia_meng')->insertGetId($addData);

        if($resId){
            return resultArray(['data' => [],'error' => '添加成功！工作人员会尽快联系您的！']);
        }

        // return $request;

    }

    public function export_jiameng(){

        $dataList = Db::name('jia_meng')->select()->toArray();

        

        $list = [];

        foreach ($dataList as $key => $value) {
            
            $one = [];

            $one['name'] = $value['name'] ?? '';

            $one['phone'] = $value['phone'] ?? '';

            $one['address'] = $value['address'] ?? '';

            $one['time'] = $value['time'] ?? '';

            $one['create_time'] = $value['create_time'] > 0 ? date('Y-m-d H:i:s',$value['create_time']) : '';

            array_push($list,$one);

        }

        
        // exit;

        $callName = [

            'name'   =>'姓名',

            'phone' =>'手机',

            'address' =>'地址',

            'time' =>'时间',

            'create_time' =>'新增时间',

        ];

        // echo "<pre>";
        // print_r($callName);
        // echo "</pre>";
        // exit;

        $downUrl = exportExcelData('jiameng',$callName,$list,'xlsx',1);

        echo "<script language='javascript'>";

        echo " location='".$downUrl."';";

        echo "</script>"; 

    }

}